<?php

declare(strict_types=1);

namespace App\Services;

use Hyperf\Di\Annotation\Inject;
use App\Exception\ApiException;
use App\Constants\ErrorCode;
use App\Model\Activity;

class ActivityService extends BaseService
{

    public function list(){
        $model = Activity::query()->select('id','title','image','content','begin_time','end_time','is_new','created_at');
        $list = $this->usePage($model)->toArray();

        foreach($list as $key=>$val)
        {
            $list[$key]['content'] = mb_substr($content, 0, 100);
            $list[$key]['status'] = check_time_status($val['begin_time'], $val['end_time']);
        }

        return $list;
    }

    public function detail(array $params)
    {
        $activity = Activity::query()->select('id','title','image','content','begin_time','end_time','is_new','created_at')->where('id', $params['id'])->first();
        if(!$activity)
        {
            throw new ApiException(ErrorCode::WARNING, '活动不存在');
        }
        $activity['status'] = check_time_status($activity['begin_time'], $activity['end_time']);

        return $activity;
    }

}